// component/polling/index.ts
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { store } from "../../store/index"
import { routingInspection } from '../../api/clockin'
ComponentWithStore({
  storeBindings: {
    store: store,
    fields: ["signinName", "HrzId", "HrzType",],
    actions: {
      updateName: "updateName",
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    inputOption: {
      maxHeight: 500,
      minHeight: 100
    },
    isLoading: false,
    fileList: [],
    powerSupply: '',
    temperature: '',
    pressure: '',
    traffic: '',
    filling: '',
    cabinet: '',
    record: '',
    id: -1,
    comID: -1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    afterRead(event: any) {
      const { file } = event.detail;
      const _this = this
      const { fileList = [] } = _this.data;
      fileList.push({ ...file, url: file.url });
      _this.setData({ fileList });
      console.log(file.url)
      // wx.uploadFile({
      //     // url: 'http://192.168.2.48:8080/MobileLinkCenter/picture/pictureUpload', // 仅为示例，非真实的接口地址
      //     url:'https://www.hbdinkon.cn:10020/MobileLinkCenter/picture/pictureUpload',
      //     filePath: file.url,
      //     name: 'file',
      //     success(res) {
      //         // 上传完成需要更新 fileList
      //         const { fileList = [] } = _this.data;
      //         fileList.push({ ...file, url: res.data });
      //         _this.setData({ fileList });
      //     },
      // });
    },
    deleteImg(e: any) {
      const index: number = e.detail.index
      this.data.fileList.splice([index], 1)
      this.setData({
        fileList: this.data.fileList
      });
      let img_list = [];
      this.data.fileList.forEach((e: any) => {
        img_list.push(e.url)
      });
    },
    async onSubmit() {
      this.setData({
        isLoading: true
      })
      const imgUrlList = this.data.fileList
      const { powerSupply,
        temperature,
        pressure,
        traffic,
        filling,
        cabinet,
        record,
        id,
        comID
      } = this.data
      let list = ''
      for (let i = 0; imgUrlList.length > i; i++) {
        const res = await this.upload(imgUrlList[i].url)
        list += JSON.parse(res.data).msg + ';'
      }
      let data = {
        siteType: store.HrzType,
        siteId: store.HrzId,
        maintainerId: id,
        comID: comID,
        powerSupply: powerSupply,
        temperature: temperature,
        pressure: pressure,
        traffic: traffic,
        filling: filling,
        cabinet: cabinet,
        record: record,
        picture: list
      }
      console.log(list)
      await console.log(data)
      const res = this.hasNoEmptyValues(data)
      if(store.signinName){
        if (res) {
          routingInspection(data).then((res: any) => {
            if (res.data.code == 200) {
              wx.showToast({
                title: '提交成功'
              })
              this.setData({
                isLoading: false,
                powerSupply: '',
                temperature: '',
                pressure: '',
                traffic: '',
                filling: '',
                cabinet: '',
                record: '',
                fileList:[]
              })
            } else {
              wx.showToast({
                title: '提交失败',
                icon: 'error'
              })
              this.setData({
                isLoading: false
              })
            }
          }).catch(err => {
            this.setData({
              isLoading: false
            })
          })
        } else {
          this.setData({
            isLoading: false
          })
          wx.showToast({
            title: '缺少必填项',
            icon: 'error'
          })
        }
      }else{
        wx.showToast({
          title:'请打卡签到',
          icon:'error'
        })
      }

    },
    hasNoEmptyValues(obj: any) {
      for (let key in obj) {
        if (obj[key] === null || obj[key] === '' || obj[key] === undefined) {
          return false; // 如果找到空值，返回false
        }
      }
      return true; // 如果没有找到空值，返回true
    },
    upload(file: string) {
      return new Promise((resolve, reject) => {
        wx.uploadFile({
          // url: 'http://192.168.2.48:8080/MobileLinkCenter/picture/pictureUpload', // 仅为示例，非真实的接口地址
          url: 'https://www.hbdinkon.cn:10020/MobileLinkCenter/picture/pictureUpload',
          filePath: file,
          name: 'file',
          success(res) {
            resolve(res)
          },
          fail(error) {
            reject(error);
          },
        });
      })
    },
    history(){
        wx.navigateTo({
          url:'/subpackagesA/pages/patrolhistory/patrolhistory'
        })
    }
  },
  pageLifetimes: {
    show: async function () {
      // 页面被展示
      this.setData({
        id: wx.getStorageSync("userInfo").data.userID,
        comID: wx.getStorageSync("userInfo").data.comID
      })
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    },
  },
})